<?php
/**
 * Created by PhpStorm.
 * User: lullabies
 * Date: 2019/5/29
 * Time: 11:14
 */

namespace app\admin\controller;
use app\Admin\model\Subjects as SubjectsModel;
use app\Admin\model\Grade as GradeModel;
use app\Admin\model\Works as WorksModel;
use app\Admin\validate\ExamSubject;
header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept,fs-token");
header('Access-Control-Allow-Methods: GET, POST');
/**
 * @title 作品管理
 * @group Admin
 */
class Work extends Base
{
    /**
     * @title 根据考试id和科目名称 获得未评分作品
     * @author 开发者
     * @url /admin/work/getWorkByExamSubject
     * @method POST/GET
     * @description 测试的考试id为1科目名称色彩，默认按作品提交时间远近排列，可自行设置
     * @param name:exam_id require:1 default:null type:int desc:考试id
     * @param name:name require:1 default:null type:varchar desc:科目名称
     * @param name:page require:0 default:1 type:int desc:当前页面数  默认第一页
     * @param name:page_size require:0 default:10 type:int desc:每页显示记录数 默认10条
     * @param name:sort require:0 default:create_time type:vatchar desc:排序关键字 默认更新时间
     * @param name:order require:0 default:desc type:varchar desc:默认排序为升序
     * @return id:作品id
     * @return img:作品图片地址
     * @return subject_id:考试科目id
     * @return user_id: 用户id
     * @return create_time:创建时间
     * @return  total_page:总页数
     */
    public function getWorkByExamSubject(){
        $data = I('exam_id|name');
        (new ExamSubject())->goCheck();
        //根据考试id 可科目名称获取科目id
        $subject = SubjectsModel::where($data)->find();
        $subject_id = $subject['id'];
        if(!$subject_id)$this->apiReturn(303,'未查询到相关科目！');
        //根据科目ID 获取已打分用户信息
        $result = GradeModel::where('subject_id',$subject_id)->field('user_id')->select();
        //获取已打分作品的用户id
        $user_id = array();
        foreach ($result as $v){
            $user_id[]=$v['user_id'];
        }
        //获得前端显示的当前页数
        $page = input('page', 1, 'intval');
        //让前端设置数据一页显示几条数据
        $page_size = input('page_size', 10, 'intval');
        //排序的关键字
        $sort = input('sort','create_time');
        //显示顺序
        $order = input('order','asc');
        $work = WorksModel::subject($subject_id)->User($user_id)
            ->page($page,$page_size)
            ->order($sort,$order)
            ->select();
        //获取查询的总记录数
        $total = count($work);
        if(!$work){
            $this->apiReturn(303,'未查询到未打分作品！');
        }else{
            $this->apiReturn(200,'查询成功',$work,$total);
        }
    }
}